from functools import reduce  # 导入模块的语法

# 内置类和内置函数都在builtins.py里
"""
    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
    """
print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]))

students = [{'name': 'AD', 'age': 23, 'score': 98},
            {'name': 'BC', 'age': 13, 'score': 56},
            {'name': 'C', 'age': 34, 'score': 67},
            {'name': 'D', 'age': 16, 'score': 86},
            {'name': 'E', 'age': 99, 'score': 64},
            {'name': 'F', 'age': 25, 'score': 89}]
sum_ages = (reduce(lambda x, y: x + y['age'], students, 0))
print(sum_ages)
# reduce(lambda x, y: x + y['age'], students, 0)
# init: x = 0, y = students[0]['age']
# reduce(lambda x, y: x + y['age'], students, 10)
# init: x = 10, y = students[0]['age']
